using IGeekFan.AspNetCore.Knife4jUI;
using Microsoft.EntityFrameworkCore;
using NLog.Web;
using Order.Api.Read.Service.RefundService;
using Order.Api.Read.Service.DrugOrdersService;
using Order.Api.Read.Service.PrescriptionOrderService;
using Order.Api.Read.Service.InterrogationOrderService;
using Order.Api.Read.Service.MedicalAIService;
using Order.Interstructrue;
using Order.Interstructrue.Base;
using System.Reflection;
using Yitter.IdGenerator;

// 创建Web应用程序构建器
var builder = WebApplication.CreateBuilder(args);

// 添加服务到容器中

// 添加控制器服务
builder.Services.AddControllers();
// 了解更多关于配置Swagger/OpenAPI的信息：https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();

// 配置Swagger文档生成
builder.Services.AddSwaggerGen(x =>
{
    var path = Path.Combine(AppContext.BaseDirectory, "Order.Api.Read.xml");
    x.IncludeXmlComments(path, true); // 包含XML注释
});

// 配置数据库上下文
builder.Services.AddDbContext<MedicalDbContext>(x =>
{
    x.UseMySql(builder.Configuration.GetConnectionString("MySQL"), new MySqlServerVersion("5.7"));
});

// 注册仓储服务
builder.Services.AddScoped(typeof(IBaseRepository<>), typeof(BaseRepository<>));
//builder.Services.AddScoped(typeof(IWmsService), typeof(WmsService));
builder.Services.AddScoped(typeof(IRefundService), typeof(RefundService));
// 注册退款服务

// 注册药品订单服务
builder.Services.AddScoped(typeof(IDrugOrdersService), typeof(DrugOrdersService));

// 注册处方订单服务
builder.Services.AddScoped(typeof(IPrescriptionOrderService), typeof(PrescriptionOrderService));

// 注册问诊订单服务
builder.Services.AddScoped(typeof(IInterrogationOrderService), typeof(InterrogationOrderService));

// 注册挂号订单服务
builder.Services.AddScoped(typeof(Order.Api.Read.Service.RegistrationOrderService.IRegistrationOrderService), typeof(Order.Api.Read.Service.RegistrationOrderService.RegistrationOrderService));

// 注册医疗AI服务
builder.Services.AddHttpClient<IMedicalAIService, OllamaAIService>();
builder.Services.AddScoped<IMedicalAIService, OllamaAIService>();

// 使用模拟AI服务（用于测试）
//builder.Services.AddScoped<IMedicalAIService, MockAIService>();

// 配置MediatR（中介者模式）
builder.Services.AddMediatR(cfg => cfg.RegisterServicesFromAssembly(Assembly.GetExecutingAssembly()));

// 配置NLog日志
builder.Host.UseNLog();

// 配置雪花ID生成器
YitIdHelper.SetIdGenerator(new IdGeneratorOptions());

// 配置AutoMapper对象映射
builder.Services.AddAutoMapper(typeof(Order.Api.Read.AutoMapper.OrderMapper));

// 构建Web应用程序
var app = builder.Build();

// 开发环境配置
if (app.Environment.IsDevelopment())
{
    app.UseSwagger(); // 启用Swagger
    app.UseKnife4UI(); // 启用Knife4j UI
    app.UseSwaggerUI(); // 启用Swagger UI
}

// 配置CORS（跨域资源共享）
app.UseCors(x => x.AllowAnyHeader().AllowAnyOrigin().AllowAnyMethod());

// 启用HTTPS重定向
app.UseHttpsRedirection();

// 启用静态文件服务
app.UseStaticFiles();

// 启用授权中间件
app.UseAuthorization();

// 映射控制器路由
app.MapControllers();

// 启动应用程序
app.Run();
